/* 四人小队框架控制 */
import creep from "@/mount/creep"
import {  identifyGarrison } from "@/prototype/creep/Misson/constant"
import { generateID, isInArray } from "@/utils"
import { AttackBody, ChangeArray, idenfityPosDirection, identifyArrivedRoom, identifyReady, SquardActive, SquardCreep } from "./base"
import {  squardMove, squardNear, squardNearNum } from "./move/move"
import { initSquard, squardAttackOrient, squardColorFlagRange, squardSteady, squardXRaction } from "./work/action"
import { GetSquardPosion, getStandCreep, squardArrivedRoom, squardAttackDirection, squardIsActive, squardPosDirection, squardReady } from "./work/state"

// 主程序执行
export function SquardManager():void{
    if (!Memory.squardMemory) Memory.squardMemory = {}
    for (var squardID in Memory.squardMemory)
    {
        /* 先检查该任务的爬是否已经死光，如果死光了就清除数据 */
        for (var creepName in Memory.squardMemory[squardID].creepData)
        {
            if (!Game.creeps[creepName]) delete Memory.squardMemory[squardID].creepData[creepName]
        }
        if (Object.keys(Memory.squardMemory[squardID].creepData).length <= 0)
        {
            delete Memory.squardMemory[squardID]
        }
        else
        {
            /* 还有活虫就按执行 */
            squardFrameWork(squardID)
        }
    }
}

// 小队通用执行框架
export function squardFrameWork(squardID:string):void{
    var Data = Memory.squardMemory[squardID]
    if (!Data) return
    /* 小队Memory中的爬虫数据 */
    var squardData =Data.creepData
    /* 小队行为*/
    squardXRaction(squardData)
    /* 如果小队已经缺员，要进行的操作 */
    if (!squardIsActive(squardData))
    {
        return
    }
    /* 如果小队没有组队或者脱离组队，要进行的操作 */
    if (!Data.ready)
    {
        if (!squardReady(squardData))
        {
            squardSteady(squardData)
        }
        else
        {
            Data.ready = true
        }
        // 组队函数 暂缺
        return
    }
    /* 如果小队因为某些原因脱离了组队，需要赶紧组队 */
    if (!squardReady(squardData))
    {
        // 组队函数 暂缺
        squardSteady(squardData)
        Data.ready = false
        return
    }
    /* 如果小队还没有到目标房间 */
    if (!squardArrivedRoom(squardData,Data.disRoom))
    {
        /* 如果有蓝色旗帜，优先去蓝色旗帜那里 */
        var blueFlag = squardColorFlagRange(squardData,COLOR_BLUE)
        if (blueFlag)
        {
            squardMove(squardData,blueFlag.pos,0)
            if(squardNear(squardData,blueFlag.pos))
            {
                blueFlag.remove()
            }
            return
        }
        /* 优先调整坐标 */
        if (!Data.init)
        {
            Data.init = true
            initSquard(Data.presentRoom,Data.disRoom,squardData)
            return
        }
        squardMove(squardData,new RoomPosition(25,25,Data.disRoom),10)
        return
    }
    /* 蓝旗是紧急撤退标志 */
    var blueFlag = squardColorFlagRange(squardData,COLOR_BLUE)
    if (blueFlag)
    {
        squardMove(squardData,blueFlag.pos,0)
        if(squardNear(squardData,blueFlag.pos))
        {
            blueFlag.remove()
        }
        return
    }

}
